// pages/competitive/competitive.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import { setTabBar } from '../../../utils/util'
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  getBanner,
  pindao,
  search,
} from '../../../utils/imapi.js';
import timer from '../../../utils/time.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [],
    show: 0,
    is_bigin: false,
    tabIndex: 0,
    currentIndicator: 0,
    bgColor: 'white',
    show_title: false,
    competitive_text: 'competitiveA',
    tabCur1: 0,
    carousel: [],
    tabData1: [
      { name: "限时购", tip: '分秒必争', id: '1', isAll: false },
      //  {name:"今日新品",tip:'新货开抢',id:'109',isAll:false},
      { name: "今日爆款", tip: '新货开抢', id: '109', isAll: false },
      //  {name:"热销爆品",tip:'猜你喜欢',id:'22',isAll:false},
      //  {name:"秒杀",tip:'疯狂抢购',id:'33',isAll:false},
      //  {name:"9.9专场",tip:'低价好货',id:'10',isAll:false},
      { name: "日用", tip: '齐全', id: '27', isAll: true },
      { name: "美妆", tip: '女神', id: '28', isAll: true },
      { name: "穿搭", tip: '潮流', id: '28', isAll: true },
      { name: "母婴", tip: '可靠', id: '26', isAll: true },
      { name: "玩具", tip: '多样', id: '26', isAll: true },
      { name: "数码", tip: '科技', id: '24', isAll: true },
      { name: "家电", tip: '实惠', id: '24', isAll: true },
      { name: "超市", tip: '居家', id: '25', isAll: true },
      { name: "家具", tip: '高端', id: '27', isAll: true },
      { name: "医药保健", tip: '健康安全', id: '29', isAll: true },
      { name: "图书文具", tip: '正品低价', id: '30', isAll: true }


    ],
    hor: '00',
    min: '00',
    sec: '00',
    size: 70,
    wsize: 750,
    color: "#ff4158",
    showMenu: 0,
    cur_scroll_top: 0,
    items: [
      { name: '蓝', value: '#3F82FD', checked: 'true' },
      { name: '红', value: '#ff4158' },
    ],
    productList: [],
    menuFixed: false,
    menuTop: 0,
    clock: 0,
    img_list: "img_list.img_list",
    bottom:app.globalData.isIphonex==1?'100rpx':'10rpx',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      img_list: img_list.img_list
    })


    console.log('=====onLoad:',options)

    //1-好券商品,2-超级大卖场,10-9.9专区,22-热销爆品,23-为你推荐,24-数码家电,25-超市,26-母婴玩具,27-家具日用,28-美妆穿搭,29-医药保健,30-图书文具,31-今日必推,32-品牌好货,33-秒杀商品,34-拼购商品,40-高收益,41-自营热卖榜,109-新品首发,110-自营,125-首购商品,129-高佣榜单,130-视频商品
    let top_banner = await getBanner({ group_code: 'jingxuan' });
    var curTime = new Date()
    shareFun.hidenShareMenu();
    var hours = parseInt((curTime.getHours() + 1) / 2) == 0 ? 12 : parseInt((curTime.getHours() + 1) / 2);
    const { data } = await pindao({ elite_id: 1, page_index: hours, page_size: 10 })
    this.page = 1;
    this.scrollTop = 0;
    this.canLoadMore = true;
    this.setData({
      carousel: top_banner.data,
      productList: data,
      CustomBar: app.globalData.CustomBar,
    })
    //开抢时间 例如2  代表2点到3点
    var time_arr = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
    var timestamp = Date.parse(curTime);
    timer.countdown(this, timestamp, time_arr);
  },
  swiperChange(e) {
    this.setData({
      currentIndicator: e.detail.current
    })
  },
  goHot() {
    wx.navigateTo({
      url: '/pages/module/hot/hot',
    })
  },
  onPageScroll: function (e) {
    var that = this;
    this.scrollTop = e.scrollTop
    if (e.scrollTop >= this.data.menuTop) {
      if (e.scrollTop > this.data.menuTop && this.data.menuFixed) return;
      that.setData({
        menuFixed: true
      })
    } else {
      that.setData({
        menuFixed: false,
        cur_scroll_top: e.scrollTop
      })
    }
  },
  async tabChange(e) {
    if (this.scrollTop >= this.data.menuTop) {
      wx.pageScrollTo({
        scrollTop: this.data.menuTop,
      })
    }
    if (this.data.tabCur1 == e.detail.index) return;
    this.setData({
      tabCur1: e.detail.index
    })
    this.page = 1
    this.canLoadMore = true;
    var curTime = new Date()
    var hours = parseInt((curTime.getHours() + 1) / 2) == 0 ? 12 : parseInt((curTime.getHours() + 1) / 2);
    if (this.data.tabData1[e.detail.index].isAll) {
      let { data } = await search({ q: this.data.tabData1[e.detail.index].name, p: '', page_index: e.detail.index == 0 ? hours : this.page, page_size: 10 })
      this.setData({
        productList: data,
        CustomBar: app.globalData.CustomBar,
      })
      return;
    }
    let { data } = await pindao({
      elite_id: this.data.tabData1[e.detail.index].id,
      page_index: e.detail.index == 0 ? hours : this.page,
      page_size: 10
    })
    this.setData({
      productList: data,
      CustomBar: app.globalData.CustomBar,
    })
  },
  async changeX() {
    if (this.data.tabCur1 != 0) return;
    var curTime = new Date()
    var hours = parseInt((curTime.getHours() + 1) / 2) == 0 ? 12 : parseInt((curTime.getHours() + 1) / 2);
    const { data } = await pindao({
      elite_id: this.data.tabData1[0].id,
      page_index: hours,
      page_size: 10
    })
    this.setData({
      productList: data,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    const query = wx.createSelectorQuery();
    query.select('.tab-wrap').boundingClientRect();
    query.exec((res) => {
      that.setData({
        menuTop: res[0].top - app.globalData.CustomBar,
        menuHeight: res[0].height
      })
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (query) {
    setTabBar.call(this, 2)


    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: async function () {
    wx.showNavigationBarLoading();
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 500);
    this.page = 1
    this.canLoadMore = true;
    if (this.data.tabData1[this.data.tabCur1].isAll) {
      let { data } = await search({ q: this.data.tabData1[this.data.tabCur1].name, p: '', page_index: this.data.tabCur1 == 0 ? hours : this.page, page_size: 10 })
      this.setData({
        productList: data,
      })
      return;
    }
    let { data } = await pindao({ elite_id: this.data.tabData1[this.data.tabCur1].id, page_index: this.data.tabCur1 == 0 ? hours : this.page, page_size: 10 })
    this.setData({
      productList: data,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    var that = this;
    if (!this.canLoadMore || this.data.tabCur1 == 0) return;
    wx.showLoading({
      title: this.page == 1 ? '加载中...' : '加载更多...',
    });
    if (this.data.tabData1[this.data.tabCur1].isAll) {
      let { data } = await search({ q: this.data.tabData1[this.data.tabCur1].name, p: '', page_index: this.page + 1, page_size: 10 })
      let arr = data;
      if (arr.length > 0) {
        this.page++;
        this.setData({
          productList: [...this.data.productList, ...arr],
        })
      } else {
        this.canLoadMore = false;
      }
      wx.hideLoading();
      return;
    }
    let { data } = await pindao({ elite_id: this.data.tabData1[this.data.tabCur1].id, page_index: this.page + 1, page_size: 10 })
    let arr = data;
    if (arr.length > 0) {
      this.page++;
      this.setData({
        productList: [...this.data.productList, ...arr],
      })
    } else {
      this.canLoadMore = false;
    }
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  async navigate(e) {
    let item = e.currentTarget.dataset
    if (item.nav_type == "weapp") {
      wx.navigateToMiniProgram({
        appId: item.app_id,
        path: item.link_url,
      })
    } else if(item.nav_type=='web') {
      wx.navigateTo({
        url: '/pages/common/h5/webview/webview?url=' +item.link_url
      })
    }
    //await saveRecord({user_id:app.globalData.uid,adv_id:this.data.active.id});
  }
})